package com.chainyoung.component_tmperson.ui.activity.debit;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.common_widget.loadsir.callback.Callback;
import com.chainyoung.common.common_widget.loadsir.core.LoadService;
import com.chainyoung.common.common_widget.loadsir.core.LoadSir;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.custom_widget.EmptyCallback;
import com.chainyoung.common.custom_widget.LoadingCallback;
import com.chainyoung.common.custom_widget.RetryCallback;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.entity.DebitRepay;
import com.chainyoung.component_tmperson.entity.DebitRepayRecord;
import com.chainyoung.component_tmperson.entity.WalletDebit;
import com.chainyoung.component_tmperson.ui.adapter.WalletDebitRepayRecordAdapter;
import com.chainyoung.component_tmperson.ui.adapter.WalletRepaymentScheduleAdapter;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletDebitRepayRecordContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.WalletDebitRepayRecordPresenter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WalletDebitRepayRecordActivity extends BaseActivity<WalletDebitRepayRecordPresenter> implements WalletDebitRepayRecordContract.View {


    @BindView(R2.id.tv_repay_amount)
    TextView tvRepayAmount;
    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.tv_coin_type)
    TextView tvCoinType;
    @BindView(R2.id.ll_list)
    LinearLayout llList;
    @BindView(R2.id.ll_top)
    LinearLayout llTop;

    private String debitId;
    private List<DebitRepayRecord> mData = new ArrayList<>();
    private WalletDebitRepayRecordAdapter adapter;
    private WalletDebit walletDebit;

    private LoadService loadService;

    @Override

    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPersonActivityComponent.builder()
                .appComponent(appComponent)
                .personActivityModule(new PersonActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUI() {
        setCurrentTitle("还款记录");
        debitId = getIntent().getStringExtra("debitId");
        walletDebit = (WalletDebit) getIntent().getSerializableExtra("walletDebit");
        initLoadSir();
        initRecylerview();
    }

    private void initLoadSir() {
        LoadSir loadSir = new LoadSir.Builder()
                .addCallback(new LoadingCallback())
                .addCallback(new EmptyCallback())
                .addCallback(new RetryCallback())
                .setDefaultCallback(LoadingCallback.class)
                .build();
        loadService = loadSir.register(llList, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                getData();
            }
        });
    }

    private void initViews(DebitRepay data) {
        llTop.setVisibility(new BigDecimal(data.getNext_repay().getRepay_amount()).compareTo(BigDecimal.ZERO)<1?View.GONE:View.VISIBLE);
        tvCoinType.setText(data.getNext_repay().getRepay_day() + "应还" + walletDebit.getCoin().toUpperCase());
        tvRepayAmount.setText(StringUtil.subZeroAndDot(data.getNext_repay().getRepay_amount()));
    }

    private void initRecylerview() {
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new WalletDebitRepayRecordAdapter(mData);
        rvList.setAdapter(adapter);
        DividerItemDecoration itemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.shape_divider_gray));
        rvList.addItemDecoration(itemDecoration);
    }


    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put(LibConstant.TOKEN, SPUtil.getString(LibConstant.TOKEN));
        map.put("loans_id", debitId);
        mPresenter.getRepaymentScheduleList(map);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_wallet_debit_repay_record;
    }


    @Override
    public void onGetOwnWalletRecordSuccessed(DebitRepay data) {
        mData.clear();
        if (data != null && !data.getList().isEmpty()) {
            mData.addAll(data.getList());
        }
        if (data!=null){
            initViews(data);
        }
        adapter.notifyDataSetChanged();
        if (mData.isEmpty()){
            loadService.showCallback(EmptyCallback.class);
        }else {
            loadService.showSuccess();
        }
    }

    @Override
    public void onGetOwnWalletRecordFailed() {
        loadService.showCallback(RetryCallback.class);
    }
}
